﻿using System.Collections.Generic;
using System.Configuration;

namespace DrCarDrive.Util
{
    public class ApplicationSettings
    {
        private static readonly string DOCTORCAR = "doctorcarid";

        private static readonly string CONSTR = "PersonalhrDB";
        private static readonly string DB = "personalhr";
        private static readonly string SERVER = "localhost";
        private static readonly string DBUSER = "root";
        private static readonly string DBPASS = "root";

        public static string DOCTORCARID = ConfigurationManager.AppSettings[DOCTORCAR];
        //public static string CONNECTIONSTRING = ConfigurationManager.ConnectionStrings[CONSTR].ConnectionString;
        //private static EntityConnectionStringBuilder _entityConnectionStringBuilder = new EntityConnectionStringBuilder(CONNECTIONSTRING);

        //private static MySqlConnectionStringBuilder _mySqlConnectionString = new MySqlConnectionStringBuilder(_entityConnectionStringBuilder.ProviderConnectionString);
        
        //public static MySqlConnectionStringBuilder MySqlConnectionString
        //{
        //    get
        //    {
        //        return _mySqlConnectionString;
        //    }
        //} 

        public ApplicationSettings()
        {

        }

        public static IList<string> UpdateValues(string doctorCarId)
        {
            List<string>  ErrorList = new List<string>();

            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings[ApplicationSettings.DOCTORCAR].Value = doctorCarId;

                ApplicationSettings.DOCTORCARID = doctorCarId;

                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");

                //ConfigurationManager.ConnectionStrings[CONSTR].ConnectionString;
            }
            catch (System.Exception)
            {
                ErrorList.Add(Messages.ERR_DOCTORCAR_UPDATE_FAILED);
            }

            return ErrorList;
        }

        public static IList<string> UpdateDataBase(string server, string database, string user, string password)
        {
            List<string> ErrorList = new List<string>();

            try
            {
                //_mySqlConnectionString.Database = database;
                //_mySqlConnectionString.Server = server;
                //_mySqlConnectionString.UserID = user;
                //_mySqlConnectionString.Password = password;

                //_entityConnectionStringBuilder.ProviderConnectionString = _mySqlConnectionString.ToString();

                //Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                //config.ConnectionStrings.ConnectionStrings[ApplicationSettings.CONSTR].ConnectionString = _entityConnectionStringBuilder.ToString();
                //config.ConnectionStrings.CurrentConfiguration.ConnectionStrings.ConnectionStrings[ApplicationSettings.CONSTR].ConnectionString = _entityConnectionStringBuilder.ToString();

                //config.Save(ConfigurationSaveMode.Modified);
                //ConfigurationManager.RefreshSection("connectionStrings");
                //ConfigurationManager.GetSection("connectionStrings");
                //Properties.Settings.Default.Reload();
            }
            catch (System.Exception)
            {
                ErrorList.Add(Messages.ERR_DATABASE_UPDATE_FAILED);
            }

            return ErrorList;
        }

        public static IList<string> ResetToDefaultDataBase()
        {
            IList<string> ErrorList = UpdateDataBase(SERVER, DB, DBUSER, DBPASS);
            
            return ErrorList;
        }
    }
}
